<?php 
header("Content-type: text/html; charset=utf-8"); 
require_once('get_grade_curl.php');

include_once($_SERVER['DOCUMENT_ROOT']."/common/config.php");
$_conn = connect_mysql('wechat_grade');

$get = new get_data_curl();

if($_GET['studentId'] && $_GET['password']){
	$studentId = $_GET['studentId'];
	$password = $_GET['password'];
	$sql = "SELECT * FROM wechat_user_secure WHERE studentid = '$studentId'";
	$res = mysqli_query($_conn, $sql) or die;//(mysqli_error($_conn));
	if(mysqli_num_rows($res)){
		$result = Array(
			'error' => '您已登记过，请不要重复登记。'
		);
	}else{
		if(!$get->login($studentId, $password, '')){
			$result = Array(
				'error' => '学号不正确或密码错误！',
			);
		}else{
			Do{
				$randval1 = rand(20,30);
				$randval2 = rand(1000,9999);
				$randval3 = rand(1000,9999);
				$randval = $randval1.$randval2.$randval3;
				$sql = "SELECT * FROM wechat_user_secure WHERE secureid = '$randval'";
				$res = mysqli_query($_conn, $sql) or die;//(mysqli_error($_conn));
			}While(mysqli_num_rows($res));
			$sql = "INSERT INTO wechat_user_secure (studentid, secureid, time) VALUES ('$studentId', '$randval', NOW())";
			$res = mysqli_query($_conn, $sql) or die;//(mysqli_error($_conn));
			$result = Array(
				'error' => '',
				'secureid' => $randval
			);
		}
	}
}else{
	$result = Array(
		'error' => '信息不全！',
	);
}

$json = json_encode($result);
echo $json;
?>